package org.otherworld.frame.textwriter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import javax.swing.JFileChooser;
import javax.swing.JTextArea;

public class ActionSaveDocument implements ActionListener {
	private JTextArea area;

	public ActionSaveDocument(JTextArea area) {
		this.area = area;
	}

	public void actionPerformed(ActionEvent e) {
		String text = area.getText();
		JFileChooser fileChooser = new JFileChooser(System
				.getProperty("user.dir"));
		int returnVal = fileChooser.showSaveDialog(area);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fileChooser.getSelectedFile();
			try {
				BufferedWriter out = new BufferedWriter(new FileWriter(file));
				out.write(text);
				out.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}
}
